<template>
  <div>
    <div slot="footer" class="dialog-footer">
      <el-tree
        ref="tree"
        :data="list"
        show-checkbox
        default-expand-all
        node-key="id"
        :props="{label:'name'}"
        check-strictly
      />
      <el-button @click="$emit('close') ">取 消</el-button>
      <el-button type="primary" @click="hSubmit">确 定</el-button>
    </div>
  </div>
</template>

<script>
import { getPermissionList } from '@/api/permisson'
import { tranListToTreeData } from '@/utils'
import { assignPerm, getRoleDetail } from '@/api/user'

export default {
  name: 'VueAdminTemplateAssignPermission',
  props: {
    id: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      list: []
    }
  },

  created() {
    this.loadPermissionList()
    this.loadPermissionByRoleId()
  },

  methods: {
    async  hSubmit() {
      const ids = this.$refs.tree.getCheckedKeys()
      const res = await assignPerm({ id: this.id, permIds: ids })
      this.$message.success(res.message)
      this.$emit('close')
    },
    async loadPermissionList() {
      const res = await getPermissionList()
      this.list = tranListToTreeData(res.data)
    },
    async loadPermissionByRoleId() {
      const res = await getRoleDetail(this.id)
      this.$refs.tree.setCheckedKeys(res.data.permIds)
    },
    getCheckedKeys() {
      console.log(this.$refs.tree.getCheckedKeys())
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
